﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sitecore.Data.Items;

namespace Workflow.Alerts.Sublayouts
{
    public partial class DetailPage : System.Web.UI.UserControl
    {
        private string detailsPath;

        #region Properties
        public string DetailsPath
        {
            get { return string.Format("{0}?alertId={1}", detailsPath, AlertId); }
        }
        public string AlertId
        {
            get { return Request.QueryString["alertId"]; }
        }

        public string MapPath
        {
            get { return MapXml.Value; }
        }
        #endregion

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[WorkflowConstants.OpenList] != null)
            {
                Session[WorkflowConstants.OpenList] = null;
                Response.Redirect(Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item.Parent));
            }

            Item mapPageItem = Sitecore.Context.Item.Axes.SelectSingleItem("../mappage");
            MapXml.Value = string.Format("{0}?id={1}", Sitecore.Links.LinkManager.GetItemUrl(mapPageItem), AlertId);

            Item detailsPageItem = Sitecore.Context.Item.Axes.SelectSingleItem("../summarypage");
            detailsPath = Sitecore.Links.LinkManager.GetItemUrl(detailsPageItem);

            if ((Session["Ids"] == null) || ((Session["Ids"] as List<int>)).Count < 2)
            {
                btnNext.Enabled = false;
                btnPrev.Enabled = false;
            }

            #region Map Enabled
            Sitecore.Data.Fields.CheckboxField mapField = (Sitecore.Data.Fields.CheckboxField)Sitecore.Context.Item.Parent.Fields["Map"];
            if (mapField != null)
            {
                //check condition property on Sitecore
                if (!mapField.Checked)
                {
                    phHideMap.Visible = true;
                }
            }
            #endregion
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            var lst = Session["Ids"] as List<int>;
            int index = lst.IndexOf(Int32.Parse(AlertId));
            if (index == 0)
                index = lst.Count;
            var path = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item);
            Response.Redirect(string.Format("{0}?alertId={1}", path, lst[index - 1]));
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            var lst = Session["Ids"] as List<int>;
            int index = lst.IndexOf(Int32.Parse(AlertId));
            if (index == lst.Count - 1)
                index = -1;
            var path = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item);
            Response.Redirect(string.Format("{0}?alertId={1}", path, lst[index + 1]));
        }
        #endregion
    }
}